﻿//-----------------------------------------------------------------------
// <copyright file="FirmaController.cs" company="CronopioDD">
//     Copyright CronopioDD. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace ProyectoGoya.Controllers
{
    using System;
    using System.Data;
    using System.Linq;
    using System.Web.Mvc;
    using ProyectoGoya.Models;

    /// <summary>
    /// FirmaController class
    /// </summary>
    public class FirmaController : Controller
    {
        #region Private Members

        /// <summary>
        /// Entity Framework DB Context
        /// </summary>
        private ProyectoGOYAContext db = new ProyectoGOYAContext();

        #endregion

        #region Public Methods

        /// <summary>
        /// GET: /Firma/
        /// </summary>
        /// <returns>Index View</returns>
        public ActionResult Index()
        {
            return this.RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// GET: /Firma/AdminOnly
        /// </summary>
        /// <returns>Index View</returns>
        public ActionResult AdminOnly()
        {
            return this.View("Index", this.db.Firmas.ToList());
        }

        /// <summary>
        /// GET: /Firma/Details/5
        /// </summary>
        /// <param name="id">Selected Firma's Id</param>
        /// <returns>Details View</returns>
        public ActionResult Details(int id)
        {
            Firma firma = this.db.Firmas.Find(id);
            if (firma == null)
            {
                return this.HttpNotFound();
            }

            return this.View(firma);
        }

        /// <summary>
        /// GET: /Firma/Create
        /// </summary>
        /// <returns>Create View</returns>
        public ActionResult Create()
        {
            return this.View();
        }

        /// <summary>
        /// POST: /Firma/Create
        /// </summary>
        /// <param name="firma">Firma to be created</param>
        /// <returns>Created Firma</returns>
        [HttpPost]
        public ActionResult Create(Firma firma)
        {
            ViewBag.SaveSuccessful = false;
            if (ModelState.IsValid && firma != null)
            {
                firma.Fecha = DateTime.Now;

                this.db.Firmas.Add(firma);
                this.db.SaveChanges();
                ModelState.Clear();
                ViewBag.SaveSuccessful = true;
            }

            return this.PartialView("_SaveSuccessfulPartial");
        }

        /// <summary>
        /// GET: /Firma/Edit/5
        /// </summary>
        /// <param name="id">Selected Firma's Id</param>
        /// <returns>Edit View</returns>
        public ActionResult Edit(int id)
        {
            Firma firma = this.db.Firmas.Find(id);
            if (firma == null)
            {
                return this.HttpNotFound();
            }

            return this.View(firma);
        }

        /// <summary>
        /// POST: /Firma/Edit/5
        /// </summary>
        /// <param name="firma">Firma to be edited</param>
        /// <returns>Edited Firma</returns>
        [HttpPost]
        public ActionResult Edit(Firma firma)
        {
            if (ModelState.IsValid)
            {
                this.db.Entry(firma).State = EntityState.Modified;
                this.db.SaveChanges();

                return this.RedirectToAction("Index");
            }

            return this.View(firma);
        }

        /// <summary>
        /// GET: /Firma/Delete/5
        /// </summary>
        /// <param name="id">Selected Firma's Id</param>
        /// <returns>Delete View</returns>
        public ActionResult Delete(int id)
        {
            Firma firma = this.db.Firmas.Find(id);
            if (firma == null)
            {
                return this.HttpNotFound();
            }

            return this.View(firma);
        }

        /// <summary>
        /// POST: /Firma/Delete/5
        /// </summary>
        /// <param name="id">Selected Firma's Id</param>
        /// <returns>Deleted Firma</returns>
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Firma firma = this.db.Firmas.Find(id);
            this.db.Firmas.Remove(firma);
            this.db.SaveChanges();

            return this.RedirectToAction("Index");
        }

        #endregion

        #region Protected Methods

        /// <summary>
        /// Method for releasing all the resources that it owns
        /// </summary>
        /// <param name="disposing">How it is going to dispose</param>
        protected override void Dispose(bool disposing)
        {
            this.db.Dispose();
            base.Dispose(disposing);
        }

        #endregion
    }
}